import ds18x20
from machine import Pin
import onewire
import dht
import time
import _thread
import network
from TDSSensor import TDSSensor

class Hardware(object):
    ow = onewire.OneWire(Pin(15))
    ds_sensor = ds18x20.DS18X20(ow)
    dht_sensor = dht.DHT22(Pin(26))
    led = Pin(2, Pin.OUT)
    relay_pin1 = Pin(14, Pin.OUT)
    relay_pin2 = Pin(27, Pin.OUT)
    info = None
    ipaddress = None

    MAX_TEMP: float = 38.5
    MIN_TEMP: float = 37.0

    max_temp = MAX_TEMP
    min_temp = MIN_TEMP

    @staticmethod
    def do_connect_wifi():
        wlan = network.WLAN(network.STA_IF)
        wlan.active(True)
        if not wlan.isconnected():
            print('connecting to network...')
            wlan.connect('ALEX_WIFI', 'qm19910506')
            while not wlan.isconnected():
                Hardware.show_led()
        Hardware.ipaddress = wlan.ifconfig()[0]
        Hardware.set_led_on(False)
        return Hardware.ipaddress

    # 温度传感器
    @staticmethod
    def read_ds_sensor():
        roms = Hardware.ds_sensor.scan()
        Hardware.ds_sensor.convert_temp()
        for rom in roms:
            temp = Hardware.ds_sensor.read_temp(rom)
            return temp
        return 0

    # dht传感器 可获取水质检测和湿度
    @staticmethod
    def read_dht_sensor():

        # Hardware.dht_sensor.measure()
        # return Hardware.dht_sensor.temperature(), Hardware.dht_sensor.humidity()  # 返回温度和湿度 eg. 23.6 (°C)  41 (% RH)
        return TDSSensor.get_tds(), 65

    # led 闪烁
    @staticmethod
    def show_led():
        Hardware.led.value(1)
        time.sleep_ms(100)
        Hardware.led.value(0)
        time.sleep_ms(100)

    # 设置led状态
    @staticmethod
    def set_led_on(led_on):
        if led_on:
            Hardware.led.value(1)
        else:
            Hardware.led.value(0)

    # 继电器
    @staticmethod
    def relay1(is_open):
        if is_open:
            Hardware.relay_pin1.on()  # 低电平闭合
        else:
            Hardware.relay_pin1.off()

    # 继电器
    @staticmethod
    def relay2(is_open):
        if is_open:
            Hardware.relay_pin2.on()
        else:
            Hardware.relay_pin2.off()


    @staticmethod
    def __callback__():
        while True:
            try:
                ds_temp = Hardware.read_ds_sensor()
            except Exception:
                print("请检查引脚接线是否良好")
                time.sleep(1)
                continue

            dht, humidity = Hardware.read_dht_sensor()
            if ds_temp < Hardware.min_temp:
                Hardware.relay1(True)
                Hardware.relay2(True)
            elif ds_temp > Hardware.max_temp:
                Hardware.relay1(False)
                Hardware.relay2(False)

            # if dht_temp < Hardware.min_temp:
            #     Hardware.relay2(True)
            # elif dht_temp > Hardware.max_temp:
            #     Hardware.relay2(False)

            Hardware.info = '{"temp1" : %s,"dht":%s, "humidity":%s, "ipaddress":"%s", "min_temp":%s, "max_temp":%s}' \
                            % (ds_temp, dht, humidity, Hardware.ipaddress, Hardware.min_temp, Hardware.max_temp)
            print(Hardware.info)
            time.sleep(1)

    @staticmethod
    def task():
        _thread.start_new_thread(Hardware.__callback__, ())
        
hardwareObj = Hardware()